﻿#include "ThreadCin.h"
#include <iostream>
#include <QMutexLocker>
#include <string>

ThreadCin::ThreadCin(QVector<ThreadClienNetWork*>* targetObjs
	, QMutex* ioMutex): netWrokTargetObjs{ targetObjs },
	ioMutex{ ioMutex } { }

ThreadCin::ThreadCin(QVector<QString>* targetObjs
	, QMutex* ioMutex): normalObs{ targetObjs },
	ioMutex{ ioMutex } { }

ThreadCin::~ThreadCin( ) { }

void ThreadCin::run( ) {
	if( netWrokTargetObjs )
		while( true ) {
			std::string getStdStr;
			std::cin >> getStdStr;
			buff.clear( );
			buff = buff.fromLocal8Bit(getStdStr.c_str( ));
			emit getCin(&buff, &semaphore);
			QMutexLocker locker(ioMutex);
			semaphore.acquire(netWrokTargetObjs->size( ));
		}
	if( normalObs )
		while( true ) {
			std::string getStdStr;
			std::cin >> getStdStr;
			buff.clear( );
			buff = buff.fromLocal8Bit(getStdStr.c_str( ));
			emit getCin(&buff, &semaphore);
			int available = semaphore.available(  );
			QMutexLocker locker(ioMutex);
			semaphore.acquire(normalObs->size( ));
		}
}
